{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第$n$个斐波那契数\n",
    "\n",
    "[![Author](https://img.shields.io/badge/Author-%40mjd507-important?style=social&logo=GitHub)](https://github.com/mjd507)\n",
    "[![WeChat Channel](https://img.shields.io/badge/Wechat-Daily%20Problem-important?style=social&logo=WeChat)](https://mp.weixin.qq.com/s/a0Siaa5xz_LBIpGXYP2ocQ)\n",
    "\n",
    "![Logo](images/Day77-1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　斐波那契数列是一组按照以下递归公式定义的整数列：\n",
    "$$\n",
    "\\begin{cases}\n",
    "F(n)=F(n-1)+F(n-2) & n \\geq 2 \\\\\n",
    "F(1)=1 \\\\\n",
    "F(0)=1\n",
    "\\end{cases}\n",
    "$$\n",
    "换句话说，第$n$项斐波那契数是前$2$项的和。以下是斐波那契数列的前$13$项：\n",
    "$$\n",
    "0,1,1,2,3,5,8,13,21,34,55,89,144,\\cdots\n",
    "$$\n",
    "\n",
    "　　给定一个数$n$，打印斐波那契数列的第$n$项。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用例说明\n",
    "\n",
    "```python\n",
    "In[1]: n = 3\n",
    "Out[1]: 2\n",
    "\n",
    "In[2]: n = 7\n",
    "Out[2]: 13\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution():\n",
    "    def fibonacci(self, n: int) -> int:\n",
    "        if n <= 1:\n",
    "            return n\n",
    "        dynamic = [0, 1]\n",
    "        for i in range(2, n + 1):\n",
    "            dynamic[i % 2] = dynamic[(i - 2) % 2] + dynamic[(i - 1) % 2]\n",
    "        return dynamic[n % 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "13\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "print(solution.fibonacci(3))\n",
    "print(solution.fibonacci(7))\n",
    "print(solution.fibonacci(9))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
